package org.eclipse.soc.ide4edu.assignment.editors;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.soc.ide4edu.assignment.wizards.UploadWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;

public class OpenPage extends FormPage {

	IEditorInput input;
	ScrolledForm form;
	IFile file = null;
	BufferStore buffStore;
	Browser browser;
	StyledText text;

	public OpenPage(FormEditor editor) {
		super(editor, "split", "Split");
		input = editor.getEditorInput();
	}

	private void addBrowserPage(FormEditor editor, BufferStore buff) {
		BrowserPage browserPage = new BrowserPage(editor, buff);
		try {
			getEditor().addPage(browserPage);
			getEditor().setActivePage("html");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private void addTextPage(FormEditor editor, BufferStore buff) {
		TextPage textPage = new TextPage(editor, buff, this);
		try {
			getEditor().addPage(textPage);
			getEditor().setActivePage("text");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private void createChoices(final Composite composite) {
		new Label(composite, SWT.NONE).setText("View ");

		Button text = new Button(composite, SWT.RADIO);
		text.setText("Text");
		text.setSelection(true);
		text.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				displayText();
			}
		});

		Button html = new Button(composite, SWT.RADIO);
		html.setText("HTML");
		html.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				displayBrowser();
			}
		});

		Button both = new Button(composite, SWT.RADIO);
		both.setText("Both");
		both.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				displayBoth();
			}
		});

	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {

		form = managedForm.getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;

		form.getBody().setLayout(layout);

		if (input instanceof IFileEditorInput)
			file = ((IFileEditorInput) input).getFile();

		buffStore = new BufferStore(file);

		form.setText(file.getName());

		Section section = toolkit.createSection(form.getBody(),
				ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR);

		toolkit.createCompositeSeparator(section);
		section.setText("Current Details");
		Composite client = toolkit.createComposite(section, SWT.WRAP);
		layout = new GridLayout();
		layout.numColumns = 4;

		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 4;
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;

		text = new StyledText(client, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		text.setEditable(false);
		text.setLayoutData(gd);
		text.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				addTextPage(getEditor(), buffStore);
			}
		});

		browser = new Browser(client, SWT.NONE);
		browser.setLayoutData(gd);
		browser.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				addBrowserPage(getEditor(), buffStore);
			}
		});

		displayText();
		createChoices(client);

		client.setLayout(layout);
		Button editButton = toolkit.createButton(client, "Edit", SWT.PUSH);
		gd = new GridData(SWT.FILL);
		editButton.setLayoutData(gd);
		editButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleEdit();
			}

		});

		Button show = toolkit.createButton(client, "Show", SWT.PUSH);
		gd = new GridData(SWT.FILL);
		show.setLayoutData(gd);
		show.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showView();
			}

		});

		Button upload = toolkit.createButton(client, "Upload", SWT.PUSH);
		gd = new GridData(SWT.FILL);
		upload.setLayoutData(gd);
		upload.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				startUpload();
			}

		});

		Button refresh = toolkit.createButton(client, "Refresh", SWT.PUSH);
		gd = new GridData(SWT.FILL);
		refresh.setLayoutData(gd);
		refresh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				refreshPage();
			}
		});

		section.setClient(client);
		section.setExpanded(true);
		gd = new GridData(GridData.FILL_BOTH);
		section.setLayoutData(gd);

	}

	public void displayBoth() {
		buffStore.initEdit(text, getSite().getShell().getDisplay());
		browser.setText(buffStore.getText());
		browser.setVisible(true);
		text.setVisible(true);
	}

	public void displayBrowser() {
		text.setVisible(false);
		browser.setText(buffStore.getText());
		browser.setVisible(true);
	}

	public void displayText() {
		browser.setVisible(false);
		buffStore.initEdit(text, getSite().getShell().getDisplay());
		text.setVisible(true);
	}

	private void handleEdit() {
		EditorDialog dialog = new EditorDialog(form.getShell(), buffStore);
		dialog.create();
		dialog.getShell().setSize(600, 800);
		dialog.open();
		browser.setText(buffStore.getBuffer());
		form.reflow(true);
		form.redraw();

	}

	private void refreshPage() {
		browser.setText(buffStore.getText());
		buffStore.initEdit(text, getSite().getShell().getDisplay());
		getSite().getShell().layout();

	}

	private void showView() {
		try {
			PlatformUI.getWorkbench().getActiveWorkbenchWindow()
					.getActivePage().resetPerspective();
			PlatformUI.getWorkbench().getActiveWorkbenchWindow()
					.getActivePage().showView("AssignmentView", null,
							IWorkbenchPage.VIEW_ACTIVATE);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private void startUpload() {

		IWizard wizard = new UploadWizard();
		WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
		dialog.open();
	}

}
